package com.xyz.yygh.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.base.CaseFormat;
import com.google.common.base.Converter;
import com.xyz.yygh.common.result.R;
import com.xyz.yygh.common.result.Result;
import com.xyz.yygh.common.util.MD5;
import com.xyz.yygh.hosp.service.HospitalSetService;
import com.xyz.yygh.model.hosp.HospitalSet;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Random;

@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;


    @ApiOperation(value = "1.医院设置分页查询")
    @GetMapping("")
    public R index(
            @ApiParam(name = "current", value = "当前页", defaultValue = "1", required = false)
            @RequestParam(defaultValue = "1") int current,
            @ApiParam(name = "pageSize", value = "分页大小", defaultValue = "10", required = false)
            @RequestParam(defaultValue = "10") int pageSize,
            @ApiParam(name = "id", value = "id", required = false)
            @RequestParam(required = false) String id,
            @ApiParam(name = "hosname", value = "医院名称", required = false)
            @RequestParam(required = false) String hosname,
            @ApiParam(name = "hoscode", value = "医院编号", required = false)
            @RequestParam(required = false) String hoscode,
            @ApiParam(name = "sort", value = "排序字段", required = false, defaultValue = "updateTime", example = "updateTime")
            @RequestParam(required = false, defaultValue = "updateTime") String sort,
            @ApiParam(name = "order", value = "排序方式 ascend | descend", required = false, defaultValue = "descend")
            @RequestParam(required = false, defaultValue = "descend") String order
    ) {

        Page<HospitalSet> page = new Page<>(current, pageSize);
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(hosname)) {
            wrapper.like("hosname", hosname);
        }
        if (!StringUtils.isEmpty(hoscode)) {
            wrapper.like("hoscode", hoscode);
        }

        if (!StringUtils.isEmpty(sort)) {
            if ("ascend".equals(order)) {
                wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, sort));
            } else if ("descend".equals(order)) {
                wrapper.orderByDesc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, sort));
            }
        }

        if (id != null) {
            wrapper.eq("id", id);
        }


        Page<HospitalSet> pageHospitalSet = hospitalSetService.page(page, wrapper);

        return R.ok().putData(pageHospitalSet.getRecords()).putPage(pageHospitalSet.getTotal(), pageHospitalSet.getCurrent(), pageHospitalSet.getSize());
    }

    @ApiOperation(value = "2.根据id获取医院设置")
    @GetMapping("{id}")
    public R show(
            @ApiParam(name = "id", value = "医院设置id", required = true)
            @PathVariable String id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return R.ok("获取医院设置成功").putData(hospitalSet);
    }


    @ApiOperation(value = "3.新增医院设置")
    @PostMapping("")
    public R create(@RequestBody HospitalSet hospitalSet) {
        hospitalSet.setStatus(1);
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));

        boolean save = hospitalSetService.save(hospitalSet);
        if (save) {
            return R.ok("新增医院设置成功");
        } else {
            return R.error("新增医院设置失败");
        }

    }

    @ApiOperation(value = "4.修改医院设置")
    @PutMapping("")
    public R update(
            @RequestBody HospitalSet hospitalSet
    ) {
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if (flag) {
            return R.ok("修改医院设置成功");
        } else {
            return R.error("修改医院设置失败");
        }
    }

    @ApiOperation(value = "5.逻辑删除医院设置")
    @DeleteMapping("{id}")
    public R destroy(@ApiParam(name = "id", value = "医院设置id", required = true) @PathVariable Long id) {
        boolean flag = hospitalSetService.removeById(id);
        if (flag) {
            return R.ok("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    @ApiOperation(value = "6.批量逻辑删除")
    @DeleteMapping("")
    public R batchDestroy(
            @ApiParam(name = "id", value = "医院设置id", required = true, example = "[4,5]")
            @RequestBody List<Long> ids
    ) {
        System.out.println(ids);
        boolean flag = hospitalSetService.removeByIds(ids);
        if (flag) {
            return R.ok("批量逻辑删除成功");
        } else {
            return R.error("批量逻辑删除失败");
        }
    }

}
